import { fileURLToPath, URL } from 'node:url'

import { defineConfig, loadEnv } from 'vite'
import * as vite from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'

console.log('Bundler:', vite['rolldownVersion'] ? `Rolldown (version: ${vite['rolldownVersion']})` : 'Rollup');

// 引入vant-ui:4.x
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { VantResolver } from "@vant/auto-import-resolver";

// https://vite.dev/config/
export default defineConfig(({ command, mode }) => {
  // 根据当前工作目录中的 `mode` 加载 .env 文件
  // 设置第三个参数为 '' 来加载所有环境变量，而不管是否有 `VITE_` 前缀。
  // eslint-disable-next-line no-undef
  const env = loadEnv(mode, process.cwd(), 'VITE_')
  console.log('_command', command)
  console.log('_mode', mode)
  console.log('_env', env)
  return {
    base: '/kc/',
    plugins: [
      vue(),
      vueDevTools(),
      AutoImport({
        resolvers: [VantResolver()],
      }),
      Components({
        resolvers: [VantResolver()],
      })
    ],
    resolve: {
      alias: {
        '@': fileURLToPath(new URL('./src', import.meta.url))
      },
    },
    server: {
      // host: "10.64.7.6",
      // host: '192.168.101.106',
      // host: '192.168.31.160',
      port: 5173,
      base: '/kc/',
    },
    define: {
      // import.meta.env.VITE_* 是动态的运行时环境变量
      // __APP_ENV__ 是静态替换的，构建后就是固定值
      __APP_ENV__: JSON.stringify(env.VITE_APP_ENV),
    },
  }
})
